<?php

class guanjiancitongjiModule extends Model
{
    public function __construct()
    {
        parent::__construct("guanjiancitongji");
    }

    /**
     * 添加
     * @date: 2019年6月19日 下午5:28:12
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function insertone($ci)
    {
        if ($ci == "") {
            return;
        }
        $data = $this->where("ci='{$ci}' ")->one();
        if (count($data) > 0) {
            $data["cipin"] = $data["cipin"] + 1;
            $this->update($data);

        } else {
            $_data["ci"] = $ci;
            $_data["cipin"] = 1;
            $_data["leixing"] = 2;
            $_data["paixu"] = 999;
            $_data["lanmuleixing"] = 2;
            $this->insert($_data);
        }


    }

    /**
     * 从政策库中插入统计
     * @date: 2019年7月8日 下午5:29:01
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function importCiFromZhengceku()
    {
        //删除原有数据
        $this->query("DELETE from sl_guanjiancitongji where leixing<>1 ");
        $zhengcelist = (new zhengcekuModule())->all();
        foreach ($zhengcelist as $k => $v) {
            $v['biaoqian'] = ($v['biaoqian'] == null || $v['biaoqian'] == "") ? [] : explode(",", $v['biaoqian']);
            $guanjianciArray = $v["biaoqian"];
            foreach ($guanjianciArray as $k1 => $v1) {
                if ($v1 != "") {
                    $this->insertone($v1);
                }
            }
        }
    }

    public function updateleixingByids($ids)
    {
        $this->query("update sl_guanjiancitongji set leixing=1 where id in ({$ids})");
    }

    public function updateresouByids($ids)
    {
        $this->query("update sl_guanjiancitongji set resou=1 where id in ({$ids})");
    }

    /**
     * 获取全部定于栏目
     * @date: 2019年7月9日 上午12:29:15
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function getalldingyuelanmuList($yonghu = "")
    {
        $parameterM = new parameterModel();
        $dingyuejiluM = new dingyuejiluModule();
        $lanmuleixingList = $parameterM->getparameterArrayByclassid("527");
        foreach ($lanmuleixingList as $k => $v) {
            $v["lanmulist"] = $this->find("id ,ci as name,id as value ,guanjianci")->where("leixing=1 and lanmuleixing='{$v["value"]}' ")->orderBy("paixu asc ,cipin desc ,id desc")->all();
            //查询用户是否已经订阅
            foreach ($v["lanmulist"] as $k1 => $v1) {
                $temp["yonghu"] = $yonghu;
                $temp["leixing"] = $v1["value"];
                $dingyuedetail = $dingyuejiluM->where($temp)->one();
                if (count($dingyuedetail) == 0) {
                    $v["lanmulist"][$k1]["isdingyue"] = false;
                } else {
                    $v["lanmulist"][$k1]["isdingyue"] = true;
                    $v["lanmulist"][$k1]["value"] = $dingyuedetail["id"];
                }
            }
            $lanmuleixingList[$k] = $v;
        }
        return $lanmuleixingList;
    }

}